# -*- coding: utf-8 -*-
#
import socket   
import time   
import sys
import array

if __name__ == '__main__':   
    addr_str = '127.0.0.1'
    # addr_str = '192.168.1.130'
    port = 8002

    if (len(sys.argv) < 2):
        print(sys.argv[0], ' <port>')
    elif (len(sys.argv) == 2):
        try:
            v = int(sys.argv[1])
            port = v
        except Exception as e:
            print(e)
            addr_str = sys.argv[1]
    elif (len(sys.argv) == 3):
        addr_str = sys.argv[1]
        port = int(sys.argv[2])

    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1)  
    addr = (addr_str, port)
    print(addr)

    while 1:
        print('Please input your context:')
        buf = input().encode('utf-8')

        if buf != '':
            b=array.array('B', buf)
            sock.sendto(b, addr)

        if (buf == 'exit'):
            sys.exit()
